<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Manage Ad-box Image - Tesdo Admin
        </title>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_adbox.php';

        //Script Process Page
        //Get objects
        $lib_upload = new lib_upload();
        $dao_adbox = new dao_adbox();

        $upload_link = DOCUMENT_ROOT . DIR_SHARED_UPLOAD_ADBOX;
        $item_old = $dao_adbox->get();

        if (!empty($_POST['btnSave'])) {
            if ($_FILES['a_img']['size'] > 0 || !empty($item_old['img'])) {
                if ($_FILES['a_img']['size'] > 0) {
                    $lib_upload->upload_file('a_img', $upload_link);
                    $img = $_FILES['a_img']['name'];
                } else {
                    $img = $item_old['img'];
                }
                $url = $_POST['txt_url'];
                if ($dao_adbox->save($url, $img)) {
                    $success = "Change Ad-Box image successfully!";
                } else {
                    $error = "Failed";
                }
            } else {
                $error = "You must choose image to upload";
            }
        }
        
        $item = $dao_adbox->get();
        ?>

        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <h1>Manage Ad-Box Image</h1>
                    <div class="MessageArea">
                        <?php if (!empty($error)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: red;">
                                <?php echo $error; ?>
                            </span>
                        <?php } ?>
                        <?php if (!empty($success)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: green;">
                                <?php echo $success; ?>
                            </span>
                        <?php } ?>
                    </div>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"  id="form_adbox" onsubmit="call()" method="POST" enctype="multipart/form-data">
                        <div>
                            <div class="TitleArea"><b>Link:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input type="text" name="txt_url" placeholder="http://www.example.com" value="<?php $item['url'] ?>" required>
                            </div>
                            <div class="TitleArea"><b>Ad-Box Image:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input type="file" name="a_img">
                                <?php if (!empty($item)) { ?>
                                    <span>
                                        <a target="_blank" href="<?php echo $upload_link . $item['img']; ?>">Show image: <?php echo $item['img']; ?></a>
                                    </span>
                                    <div style="margin-top: 10px;">
                                        <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_ADBOX . $item['img']; ?>" style="height: 250px; width: 250px;" alt="Ad_Box Image" />
                                    </div>
                                <?php }
                                ?>
                            </div>

                            <div class="TitleArea"><b>.</b></div>
                            <div class="textBoxArea">
                                <input type="submit" name="btnSave" value="Save" class="buttonDesign">
                            </div>
                        </div>

                        <br clear="all"><br>
                    </form>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
